/**
 * 运动的物体：速度，半径，阵营
 */
class Flyrole extends Laya.Sprite {
    //动画缓存状态
    private static ANI_CACHED: boolean = false;
    //物体身体
    protected body: Laya.Animation;
    //速度
    public speed: number;
    //半径
    public radius: number;
    //血量
    protected hp: number;
    //阵营0：正义；1：反派
    public camp: number;
    //当前执行的动画名称，方便动画结束时进行操作
    protected action_now: string;
    //生命值等于0时立即改变，防止重复撞击(因为有动画的存在)
    public is_exixt: boolean = true;

    constructor(){
        super();
        this.body = new Laya.Animation();
        this.addChild(this.body);
    }

    //默认初始化
    protected _init_(): void {
        this.is_exixt = true;
        this.visible = true;
    }

    //类回收
    protected rollbackSelf():void {
        this.is_exixt = false;
    }

    //缓存动画，入口类直接调用
    static cachedAni(): boolean {
        if(!Flyrole.ANI_CACHED) {
            Flyrole.ANI_CACHED = true;
            //缓存飞行动画
            Laya.Animation.createFrames(["war/hero_fly1.png","war/hero_fly2.png"],"plane0_fly");
            //缓存击中爆炸效果
            Laya.Animation.createFrames(["war/hero_down1.png","war/hero_down2.png","war/hero_down3.png","war/hero_down4.png"],"plane0_down");
            //缓存敌机1飞行动画
            Laya.Animation.createFrames(["war/enemy1_fly1.png"],"plane1_fly");
            //缓存敌机1爆炸动画
            Laya.Animation.createFrames(["war/enemy1_down1.png","war/enemy1_down2.png","war/enemy1_down3.png","war/enemy1_down4.png"],"plane1_down");
            //缓存敌机2飞行动画
            Laya.Animation.createFrames(["war/enemy2_fly1.png"],"plane2_fly");
            //缓存敌机2爆炸动画
            Laya.Animation.createFrames(["war/enemy2_down1.png","war/enemy2_down2.png","war/enemy2_down3.png","war/enemy2_down4.png"],"plane2_down");
            //缓存敌机2碰撞动画
            Laya.Animation.createFrames(["war/enemy2_hit.png"],"plane2_hit");
            //缓存敌机3飞行动画
            Laya.Animation.createFrames(["war/enemy3_fly1.png","war/enemy3_fly2.png"],"plane3_fly");
            //缓存敌机3爆炸动画
            Laya.Animation.createFrames(["war/enemy3_down1.png","war/enemy3_down2.png","war/enemy3_down3.png","war/enemy3_down4.png","war/enemy3_down5.png","war/enemy3_down6.png"],"plane3_down");
            //缓存敌机3碰撞动画
            Laya.Animation.createFrames(["war/enemy3_hit.png"],"plane3_hit");
            //缓存子弹动画
            Laya.Animation.createFrames(["war/bullet1.png"],"bullet0_fly");
            Laya.Animation.createFrames(["war/bullet2.png"],"bullet1_fly");
            Laya.Animation.createFrames(["war/bullet3.png"],"bullet2_fly");
            //缓存强化包
            Laya.Animation.createFrames(["war/ufo1.png"],"props0_fly");
            Laya.Animation.createFrames(["war/ufo2.png"],"props1_fly");
        }
        return true;
    }

    //根据角色名和动画吗播放动画
    public playAciton(_role_name: string, _action: string, is_cyclic:boolean = true): void {
        this.body.play(0, is_cyclic, _role_name + '_'+ _action);
        let bound:Laya.Rectangle = this.body.getBounds();
        this.body.pos(-bound.width/2, -bound.height/2);
        this.action_now = _action;
    }

    //向前飞行的位置调整
    public flyForward(): void {
        //飞行位置
        this.y += this.speed;
        if((this.speed < 0 && this.y < 0) || (this.speed > 0 && this.y > Main.s_height)) {
            this.rollbackSelf();
        }
    }

    //被击中或碰撞时
    public attacked():void {
        this.hp --;
        this.is_exixt = false;  //动画期间不可被重复撞击，很短没考虑子弹连续击中
    }
}